// 云函数入口文件
const cloud = require('wx-server-sdk')

// 初始化云开发
cloud.init({
  env: 'liucheng-2gmqvcgs50e86304' // 使用指定的云环境
});

// 云函数入口函数
exports.main = async (event, context) => {
  const db = cloud.database()

  const { _id } = event

  try {
    // 检索记录是否存在
    const res = await db.collection('Parts').doc(_id).get()

    if (!res.data) {
      return {
        success: false,
        message: 'No record found with the specified _id'
      }
    }

    // 删除记录
    const deleteRes = await db.collection('Parts').doc(_id).remove()

    if (deleteRes.stats.removed === 1) {
      return {
        success: true,
        message: 'Record deleted successfully'
      }
    } else {
      return {
        success: false,
        message: 'Failed to delete the record'
      }
    }
  } catch (error) {
    return {
      success: false,
      message: 'Error: ' + error.message
    }
  }
}
